//===----------------------------------------------------------------------===//
//
//                         BusTub
//
// distinct_executor.cpp
//
// Identification: src/execution/distinct_executor.cpp
//
// Copyright (c) 2015-2021, Carnegie Mellon University Database Group
//
//===----------------------------------------------------------------------===//

/*
 * 该执行器消除从其子执行器得到的重复记录，在唯一确定的情况下，你的Distinctexecutor应该
 * 考虑输入元组的所有列
 * */

#include "execution/executors/distinct_executor.h"

namespace bustub {

DistinctExecutor::DistinctExecutor(ExecutorContext *exec_ctx, const DistinctPlanNode *plan,
                                   std::unique_ptr<AbstractExecutor> &&child_executor)
    : AbstractExecutor(exec_ctx)
    ,plan_(plan)
    ,child_executor_(std::move(child_executor)){}

void DistinctExecutor::Init() {
  child_executor_->Init();
  Tuple child_tuple;
  RID child_rid;
  while(child_executor_->Next(&child_tuple,&child_rid)){
    // 构造key
    DistinctKey dis_key;
    dis_key.distincts_.reserve(plan_->OutputSchema()->GetColumnCount());
    for(uint32_t idx=0;idx<dis_key.distincts_.capacity();++idx){
      dis_key.distincts_.push_back(child_tuple.GetValue(plan_->OutputSchema(),idx));
    }

    // 根据key进行输入
    if(map_.count(dis_key)==0){
      map_[dis_key]= child_tuple;
    }
  }
  iter_ = map_.begin();
}

bool DistinctExecutor::Next(Tuple *tuple, RID *rid) {
  if(iter_ ==map_.end()){
    return false;
  }
  *tuple = iter_->second;
  *rid = iter_->second.GetRid();
  iter_++;
  return true;
}

}  // namespace bustub
